package com.roy.galaxy.collection;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author gang.wang1903@xiaobao100.cn
 * @Date: 2019-10-09 16:52
 *  集合线程安全
 */
public class ThreadSafeCollection {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1,2,2,3);
        Set<String> hs = new HashSet();
        hs.add("世界军事");
        hs.add("兵器知识");
        hs.add("舰船知识");
        hs.add("汉和防务");
        Map<Integer ,String> map = new HashMap<>();
        map.put(1,"23e23");

        //集合类
        Collections.synchronizedList(list);
        Collections.synchronizedSet(hs);
        Collections.synchronizedMap(map);

        //j.u.c实现
        new CopyOnWriteArrayList(list);

        new CopyOnWriteArraySet(hs);

        new ConcurrentHashMap<>(map);
    }
}
